<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>open</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ I/O</a> &gt; <a href="open.html">open</a>
  </div>

  <div class="name-format">
    open
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;fstream&gt;
  void open( const char *filename );
  void open( const char *filename, openmode mode = default_mode );
</pre>

  <p>The function open() is used with file streams. It opens
  <em>filename</em> and associates it with the current stream. The
  optional <a href="../io_flags.html#mode_flags">io stream mode
  flag</a> <em>mode</em> defaults to ios::in for ifstream, ios::out for
  ofstream, and ios::in|ios::out for fstream.</p>

  <p>If open() fails, the resulting stream will evaluate to false when
  used in a Boolean expression. For example:</p>
  <pre class="example-code">
 ifstream inputStream;
 inputStream.open(&quot;file.txt&quot;);
 if( !inputStream ) {
   cerr &lt;&lt; &quot;Error opening input stream&quot; &lt;&lt; endl;
   return;
 }              
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="constructors.html">I/O Constructors</a><br>
    <a href="close.html">close</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
